package edu.sdjzu.exam.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import edu.sdjzu.exam.common.CommonResponse;
import edu.sdjzu.exam.dto.UserGroupCreateDTO;
import edu.sdjzu.exam.dto.UserGroupDTO;
import edu.sdjzu.exam.dto.UserGroupUpdateDTO;
import edu.sdjzu.exam.dto.AddUsersToGroupDTO;
import edu.sdjzu.exam.dto.RemoveUsersFromGroupDTO;
import edu.sdjzu.exam.service.group.UserGroupService;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/user-groups")
@RequiredArgsConstructor
@SaCheckLogin
@SaCheckRole("teacher")
public class UserGroupController {

    private final UserGroupService userGroupService;

    @PostMapping("/create")
    public CommonResponse<Long> createUserGroup(@Valid @RequestBody UserGroupCreateDTO createDTO) {
        try {
            Long groupId = userGroupService.createUserGroup(createDTO);
            return CommonResponse.success("用户组创建成功", groupId);
        } catch (Exception e) {
            return CommonResponse.error("创建失败：" + e.getMessage());
        }
    }

    @GetMapping("/{groupId}")
    public CommonResponse<UserGroupDTO> getUserGroup(@PathVariable Long groupId) {
        try {
            UserGroupDTO groupDTO = userGroupService.getUserGroup(groupId);
            return CommonResponse.success(groupDTO);
        } catch (Exception e) {
            return CommonResponse.error("获取失败：" + e.getMessage());
        }
    }

    @PutMapping("/{groupId}")
    public CommonResponse<String> updateUserGroup(@PathVariable Long groupId, @Valid @RequestBody UserGroupUpdateDTO updateDTO) {
        try {
            updateDTO.setGroupId(groupId); // 确保ID一致
            userGroupService.updateUserGroup(updateDTO);
            return CommonResponse.success("用户组更新成功");
        } catch (Exception e) {
            return CommonResponse.error("更新失败：" + e.getMessage());
        }
    }

    @DeleteMapping("/{groupId}")
    public CommonResponse<String> deleteUserGroup(@PathVariable Long groupId) {
        try {
            userGroupService.deleteUserGroup(groupId);
            return CommonResponse.success("用户组删除成功");
        } catch (Exception e) {
            return CommonResponse.error("删除失败：" + e.getMessage());
        }
    }

    @GetMapping("/my")
    public CommonResponse<List<UserGroupDTO>> listMyUserGroups() {
        try {
            List<UserGroupDTO> groups = userGroupService.listUserGroupsByCreator();
            return CommonResponse.success(groups);
        } catch (Exception e) {
            return CommonResponse.error("获取失败：" + e.getMessage());
        }
    }

    @PostMapping("/add-users")
    public CommonResponse<String> addUsersToGroup(@Valid @RequestBody AddUsersToGroupDTO addDTO) {
        try {
            userGroupService.addUsersToGroup(addDTO);
            return CommonResponse.success("用户添加成功");
        } catch (Exception e) {
            return CommonResponse.error("添加失败：" + e.getMessage());
        }
    }

    @PostMapping("/remove-users")
    public CommonResponse<String> removeUsersFromGroup(@Valid @RequestBody RemoveUsersFromGroupDTO removeDTO) {
        try {
            userGroupService.removeUsersFromGroup(removeDTO);
            return CommonResponse.success("用户移除成功");
        } catch (Exception e) {
            return CommonResponse.error("移除失败：" + e.getMessage());
        }
    }

    @Data
    public static class GroupIdDTO {
        @NotNull(message = "用户组ID不能为空")
        private Long groupId;
    }
}